from flask import Blueprint, render_template, request

from applications.common.curd import model_to_dicts, enable_status, disable_status
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api, fail_api, success_api

from applications.models import AdministratorModels, AdministratorOPModels
from applications.schemas import AdministratorOutSchema, AdministratorOPSchema

administrator_bp = Blueprint('adminAdministrator', __name__, url_prefix='/admin/administrator')


# 管理员信息
@administrator_bp.get('/')
# @authorize("administrator:administrator_info", log=True)
def administrator_main():
    return render_template('admin/administrator/administrator_info.html')


#   用户分页查询1
@administrator_bp.get('/data')
# @authorize("administrator:administrator_info", log=True)
def data():
    # 获取请求参数
    id = request.args.get('id', type=str)
    name = request.args.get('name', type=str)
    avatar = request.args.get('avatar', type=str)
    dept_id = request.args.get('dept_id', type=int)
    subject_at = request.args.get('subject_at', type=str)
    # 查询参数构造
    mf = ModelFilter()
    if id:
        mf.contains(field_name="id", value=id)
    if name:
        mf.contains(field_name="name", value=name)
    if avatar:
        mf.contains(field_name="avatar", value=avatar)
    if dept_id:
        mf.exact(field_name="dept_id",value=dept_id)
    if subject_at:
        mf.contains(field_name="dept_id",value=subject_at)
    # orm查询
    # 使用分页获取data需要.items
    administratorModels = AdministratorModels.query.filter(mf.get_filter(model=AdministratorModels)).layui_paginate()
    count = administratorModels.total
    return table_api(data=model_to_dicts(schema=AdministratorOutSchema, data=administratorModels.items), count=count)

#   用户分页查询2
@administrator_bp.get('/data2')
# @authorize("administrator:administrator_info", log=True)
def data2():
    # 获取请求参数
    operatorid = request.args.get('operatorid', type=int)
    operatorname = request.args.get('operatorname', type=str)
    classid = request.args.get('classid', type=int)
    classname = request.args.get('classname', type=str)
    success = request.args.get('success',type=bool)
    # 查询参数构造
    mf = ModelFilter()
    if operatorid:
        mf.exact(field_name="operatorid", value=operatorid)
    if operatorname:
        mf.contains(field_name="operatorname", value=operatorname)
    if classid:
        mf.exact(field_name="classid", value=classid)
    if classname:
        mf.contains(field_name="classname",value=classname)
    if success:
        mf.exact(field_name="operate-status", value=success)

    # orm查询
    # 使用分页获取data需要.items
    administratorOP = AdministratorOPModels.query.filter(mf.get_filter(model=AdministratorOPModels)).layui_paginate()
    count = administratorOP.total
    return table_api(data=model_to_dicts(schema=AdministratorOPSchema, data=administratorOP.items), count=count)



